home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet 2002 February / Practical Internet February 2002.iso / pc / Software / Browsing / httrack-3.09e2.exe / {app} / src / htslib.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-07  |  14.4 KB  |  461 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Subroutines .h                                         */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier librairie .h
  38.  
  39. #ifndef HTS_DEFH
  40. #define HTS_DEFH 
  41.  
  42. /* dΘfinitions globales */
  43. #include "htsglobal.h"
  44.  
  45. /* basic net definitions */
  46. #include "htsbasenet.h"
  47.  
  48. /* cookies et auth */
  49. #include "htsbauth.h"
  50.  
  51. // Attention, dΘfinition existante Θgalement dans le shell
  52. // (α modifier avec celle-ci)
  53. #define POSTTOK "?>post"
  54.  
  55. #include <stdio.h>
  56.  
  57. /*
  58. #if HTS_WIN
  59.  #include <winsock.h>
  60.  typedef SOCKET T_SOC;
  61.  typedef struct hostent FAR t_hostent;
  62. #else
  63.  typedef int T_SOC;
  64.  typedef struct hostent t_hostent;
  65. #endif
  66. */
  67.  
  68. /*
  69. #include <ctype.h>
  70. #if HTS_WIN
  71.  #include <winsock.h>
  72.  typedef SOCKET T_SOC;
  73.  // pour read
  74.  #include <io.h>
  75.  // pour FindFirstFile
  76.  #include <winbase.h>
  77. #else
  78.  typedef int T_SOC;
  79.  #define INVALID_SOCKET -1
  80.  #include <netdb.h>
  81.  #include <sys/types.h>
  82.  #include <sys/socket.h>
  83.  #include <netinet/in.h>
  84.  #include <sys/time.h>
  85.  #include <sys/ioctl.h>
  86.  // gethostname & co0
  87.  #include <unistd.h>
  88.  // inet_addr
  89.  #include <arpa/inet.h>
  90.  // pas la peine normalement..
  91. #if HTS_PLATFORM!=3
  92.  #include <sys/filio.h>
  93. #else
  94. #ifndef HTS_DO_NOT_REDEFINE_in_addr_t
  95.  typedef unsigned long in_addr_t;
  96. #endif
  97. #endif
  98.  
  99. #ifndef min
  100.  #define min(a,b) ((a)>(b)?(b):(a))
  101.  #define max(a,b) ((a)>(b)?(a):(b))
  102. #endif
  103. #define Sleep(a) { if (((a)*1000)%1000000) usleep(((a)*1000)%1000000); if (((a)*1000)/1000000) sleep(((a)*1000)/1000000); }
  104. #endif
  105. */
  106.  
  107. /*
  108. #include <stdio.h>
  109. #include <stdlib.h>
  110. #include <string.h>
  111. #include <time.h>
  112. #include <fcntl.h>
  113. // pour utimbuf
  114. #if HTS_PLATFORM!=3
  115. #include <utime.h>
  116. #else
  117. #include <utime.h>
  118. #endif
  119. */
  120.  
  121.  /*
  122. // teste ΘgalitΘ de 2 chars, case insensitive
  123. #define hichar(a) ((((a)>='a') && ((a)<='z')) ? ((a)-('a'-'A')) : (a))
  124. #define streql(a,b) (hichar(a)==hichar(b))
  125.  
  126. // caractΦre maj
  127. #define isUpperLetter(a) ( ((a) >= 'A') && ((a) <= 'Z') )
  128. */
  129. /*
  130. #if HTS_PLATFORM!=3
  131. #ifdef __cplusplus
  132. extern "C" {
  133. #endif
  134. #if HTS_PLATFORM!=2
  135. #if HTS_PLATFORM!=1
  136.  int   open      (const char *, int, ...);
  137. #endif
  138.  //int   read      (int,const char*,int);
  139.  //int   write     (int,char*,int);
  140. #endif
  141. #if HTS_PLATFORM!=1
  142.  int   close     (int);
  143.  void* calloc    (size_t,size_t);
  144.  void* malloc    (size_t);
  145.  void* realloc   (void*,size_t);
  146.  void  free      (void*);
  147. #endif
  148. #if HTS_WIN==0
  149.  void  bzero     (char*,unsigned int);
  150.  void  bcopy     (const char*,char*,unsigned int);
  151.  //int   strcasecmp(const char*,const char*);
  152. #endif
  153.  //int   strlen    (const char *);
  154. #if HTS_WIN
  155.  //int   mkdir     (const char*);
  156. #else
  157.  int   mkdir     (const char*,mode_t);
  158.  int   isdigit   (char);
  159.  int   isalpha   (char);
  160.  int   isalnum   (char);
  161. #endif
  162. #ifdef __cplusplus
  163. }
  164. #endif
  165. #endif
  166. */
  167.  
  168. /*
  169. // conversion Θventuelle / vers antislash
  170. #if HTS_WIN
  171. char* antislash(char* s);
  172. #else
  173. #define antislash(A) (A)
  174. #endif
  175. */
  176.  
  177. /*
  178. #if HTS_WIN
  179. #define  bzero(a,b) memset(a,0,b)
  180. #define  bcopy(a,b,c) memcpy(b,a,c)
  181. #endif
  182.  
  183. // tracer malloc()
  184. #if HTS_TRACE_MALLOC
  185. #define malloct(A)    hts_malloc(A,0)
  186. #define calloct(A,B)  hts_malloc(A,B)
  187. #define freet(A)      hts_free(A)
  188. #define realloct(A,B) hts_realloc(A,B)
  189. void  hts_freeall();
  190. void* hts_malloc    (size_t,size_t);
  191. void  hts_free      (void*);
  192. void* hts_realloc   (void*,size_t);
  193. #else
  194. #define malloct(A)    malloc(A)
  195. #define calloct(A,B)  calloc(A,B)
  196. #define freet(A)      free(A)
  197. #define realloct(A,B) realloc(A,B)
  198. #endif
  199. */
  200.  
  201. #include "htsopt.h"
  202.  
  203. // structure pour paramΦtres supplΘmentaires lors de la requΩte
  204. typedef struct {
  205.   short int user_agent_send;  // user agent (ex: httrack/1.0 [sun])
  206.   short int http11;           // l'en tΩte peut (doit) Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  207.   short int range_used;       // Range utilisΘ
  208.   short int nocompression;    // Pas de compression
  209.   char user_agent[64];
  210.   char lang_iso[64];
  211.   t_proxy proxy;              // proxy
  212. } htsrequest;
  213.  
  214.  
  215. // structure pour retour d'une connexion/prise d'en tΩte
  216. typedef struct {
  217.   int statuscode;        // status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  218.   short int notmodified; // page ou fichier NON modifiΘ (transfΘrΘ)
  219.   short int is_write;    // sortie sur disque (out) ou en mΘmoire (adr)
  220.   short int is_chunk;    // mode chunk
  221.   short int compressed;  // compressΘ?
  222.   char* adr;             // adresse du bloc de mΘmoire, NULL=vide
  223.   FILE* out;             // Θcriture directe sur disque (si is_write=1)
  224.   LLint size;            // taille fichier
  225.   char msg[80];          // message Θventuel si Θchec ("\0"=non prΘcisΘ)
  226.   char contenttype[64];  // content-type ("text/html" par exemple)
  227.   char contentencoding[64];  // content-encoding ("gzip" par exemple)
  228.   char* location;        // on copie dedans Θventuellement la vΘritable 'location'
  229.   LLint totalsize;       // taille totale α tΘlΘcharger (-1=inconnue)
  230.   short int is_file;     // ce n'est pas une socket mais un descripteur de fichier si 1
  231.   T_SOC soc;             // ID socket
  232.   FILE* fp;              // fichier pour file://
  233.   char lastmodified[64]; // Last-Modified
  234.   char etag[64];         // Etag
  235.   char cdispo[256];      // Content-Disposition coupΘ
  236.   /* */
  237.   htsrequest req;  // paramΦtres pour la requΩte
  238.   /*char digest[32+2];  // digest md5 gΘnΘrΘ par le moteur ("" si non gΘnΘrΘ)*/
  239. } htsblk;
  240.  
  241.  
  242. /* ANCIENNE STURCTURE pour cache 1.0 */
  243. typedef struct {
  244.   int statuscode;  // ANCIENNE STURCTURE - status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  245.   int notmodified; // ANCIENNE STURCTURE - page ou fichier NON modifiΘ (transfΘrΘ)
  246.   int is_write;    // ANCIENNE STURCTURE - sortie sur disque (out) ou en mΘmoire (adr)
  247.   char* adr;       // ANCIENNE STURCTURE - adresse du bloc de mΘmoire, NULL=vide
  248.   FILE* out;       // ANCIENNE STURCTURE - Θcriture directe sur disque (si is_write=1)
  249.   int size;        // ANCIENNE STURCTURE - taille fichier
  250.   char msg[80];    // ANCIENNE STURCTURE - message Θventuel si Θchec ("\0"=non prΘcisΘ)
  251.   char contenttype[64];  // ANCIENNE STURCTURE - content-type ("text/html" par exemple)
  252.   char* location;  // ANCIENNE STURCTURE - on copie dedans Θventuellement la vΘritable 'location'
  253.   int totalsize;   // ANCIENNE STURCTURE - taille totale α tΘlΘcharger (-1=inconnue)
  254.   int is_file;     // ANCIENNE STURCTURE - ce n'est pas une socket mais un descripteur de fichier si 1
  255.   T_SOC soc;       // ANCIENNE STURCTURE - ID socket
  256.   FILE* fp;        // ANCIENNE STURCTURE - fichier pour file://
  257.   t_proxy proxy;   // ANCIENNE STURCTURE - proxy
  258.   int user_agent_send;  // ANCIENNE STURCTURE - user agent (ex: httrack/1.0 [sun])
  259.   char user_agent[64];
  260.   int http11;           // ANCIENNE STURCTURE - l'en tΩte doit Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  261. } OLD_htsblk;
  262. /* fin ANCIENNE STURCTURE pour cache 1.0 */
  263.  
  264. // cache pour le dns, pour Θviterd de faire des gethostbyname sans arrΩt
  265. typedef struct t_dnscache {
  266.   char iadr[1024];
  267.   char host_addr[16];    // 4 octets normalement ()
  268.   int host_length;       // 4 normalement - ==0  alors en cours de rΘsolution
  269.                          //                 ==-1 alors erreur (host n'Θxiste pas)
  270.   struct t_dnscache* n;
  271. } t_dnscache;
  272.  
  273.  
  274.  
  275.  
  276. /*
  277. #ifdef __cplusplus
  278. extern "C" {
  279. #endif
  280. */
  281.  
  282. // fonctions unix/winsock
  283. int hts_read(htsblk* r,char* buff,int size);
  284. //int HTS_TOTAL_RECV_CHECK(int var);
  285. LLint check_downloadable_bytes(int rate);
  286.  
  287. int hts_init(void);
  288.  
  289. // fonctions principales
  290. int http_fopen(char* adr,char* fil,htsblk* retour);
  291. int http_xfopen(int mode,int treat,int waitconnect,char* xsend,char* adr,char* fil,htsblk* retour);
  292. int http_sendhead(t_cookie* cookie,int mode,char* xsend,char* adr,char* fil,char* referer_adr,char* referer_fil,htsblk* retour);
  293. htsblk httpget(char* url);
  294. //int newhttp(char* iadr,char* err=NULL);
  295. int newhttp(char* iadr,htsblk* retour,int port,int waitconnect);
  296. HTS_INLINE void deletehttp(htsblk* r);
  297. HTS_INLINE void deletesoc(T_SOC soc);
  298. htsblk http_location(char* adr,char* fil,char* loc);
  299. htsblk http_test(char* adr,char* fil,char* loc);
  300. int check_readinput(htsblk* r);
  301. void http_fread(T_SOC soc,htsblk* retour);
  302. LLint http_fread1(htsblk* r);
  303. void treathead(t_cookie* cookie,char* adr,char* fil,htsblk* retour,char* rcvd);
  304. void treatfirstline(htsblk* retour,char* rcvd);
  305. void infostatuscode(char* msg,int statuscode);
  306.  
  307. // sous-fonctions
  308. htsblk xhttpget(char* adr,char* fil);
  309. htsblk http_gethead(char* adr,char* fil);
  310. LLint http_xfread1(htsblk* r,int bufl);
  311. HTS_INLINE t_hostent* hts_gethostbyname(char* iadr);
  312. t_hostent* _hts_ghbn(t_dnscache* cache,char* iadr,t_hostent* retour);
  313. int ftp_available(void);
  314. #if HTS_DNSCACHE
  315. int hts_dnstest(char* _iadr);
  316. t_dnscache* _hts_cache(void);
  317. int _hts_lockdns(int i);
  318. #endif
  319.  
  320. // outils divers
  321. HTS_INLINE TStamp time_local(void);
  322. HTS_INLINE TStamp mtime_local(void);
  323. void sec2str(char *s,TStamp t);
  324. void qsec2str(char *st,TStamp t);
  325. void time_gmt_rfc822(char* s);
  326. void time_local_rfc822(char* s);
  327. struct tm* convert_time_rfc822(char* s);
  328. int set_filetime(char* file,struct tm* tm_time);
  329. int set_filetime_rfc822(char* file,char* date);
  330. HTS_INLINE void time_rfc822(char* s,struct tm * A);
  331. HTS_INLINE void time_rfc822_local(char* s,struct tm * A);
  332. char* int2char(int n);
  333. char* int2bytes(LLint n);
  334. char* int2bytessec(long int n);
  335. char** int2bytes2(LLint n);
  336. HTS_INLINE int sendc(T_SOC soc,char* s);
  337. void finput(int fd,char* s,int max);
  338. int binput(char* buff,char* s,int max);
  339. void linput(FILE* fp,char* s,int max);
  340. void linput_trim(FILE* fp,char* s,int max);
  341. void linput_cpp(FILE* fp,char* s,int max);
  342. void rawlinput(FILE* fp,char* s,int max);
  343. int strfield(const char* f,const char* s);
  344. #define strfield2(f,s) ( (strlen(f)!=strlen(s)) ? 0 : (strfield(f,s)) )
  345. char* strstrcase(char *s,char *o);
  346. int ident_url_absolute(char* url,char* adr,char* fil);
  347. void fil_simplifie(char* f);
  348. int ishtml(char* urlfil);
  349. int ishtml_ext(char* a);
  350. int ishttperror(int err);
  351. void guess_httptype(char *s,char *fil);
  352. void get_httptype(char *s,char *fil,int flag);
  353. int get_userhttptype(int setdefs,char *s,char *ext);
  354. void give_mimext(char *s,char *st);
  355. int is_knowntype(char *fil);
  356. int is_userknowntype(char *fil);
  357. int is_dyntype(char *fil);
  358. char* get_ext(char *fil);
  359. int may_unknown(char* st);
  360. char* jump_identification(char*);
  361. HTS_INLINE char* jump_protocol(char* source);
  362. void code64(char* a,char* b);
  363. void unescape_amp(char* s);
  364. void escape_spc_url(char* s);
  365. void escape_in_url(char* s);
  366. void escape_uri(char* s);
  367. void escape_check_url(char* s);
  368. void x_escape_http(char* s,int mode);
  369. HTS_INLINE int ehexh(char c);
  370. char* unescape_http(char* s);
  371. char* unescape_http_unharm(char* s);
  372. char* antislash_unescaped(char* s);
  373. int ehex(char* s);
  374. char* concat(const char* a,const char* b);
  375. #define copychar(a) concat((a),NULL)
  376. #if HTS_DOSNAME
  377. char* fconcat(char* a,char* b);
  378. char* fconv(char* a);
  379. #else
  380. #define fconv(a) (a)
  381. #define fconcat(a,b) concat(a,b)
  382. #endif
  383. char* fslash(char* a);
  384. char* __fslash(char* a);
  385.  
  386. char* convtolower(char* a);
  387. char* concat(const char* a,const char* b);
  388. void hts_lowcase(char* s);
  389. void hts_replace(char *s,char from,char to);
  390.  
  391. /* Spaces: CR,LF,TAB,FF */
  392. #define  is_space(c)      ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)=='\'') )
  393. #define  is_realspace(c)  ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12)                )
  394. #define  is_taborspace(c) ( ((c)==' ')                                          || ((c)==9)                             )
  395. #define  is_quote(c)      (               ((c)=='\"')                                                    || ((c)=='\'') )
  396. //HTS_INLINE int is_space(char);
  397. //HTS_INLINE int is_realspace(char);
  398.  
  399. void fprintfio(FILE* fp,char* buff,char* prefix);
  400.  
  401. void cut_path(char* fullpath,char* path,char* pname);
  402. int fexist(char* s);
  403. /*LLint fsize(char* s);    */
  404. int fpsize(FILE* fp);
  405. int fsize(char* s);    
  406. /* root dir */
  407. char* hts_rootdir(char* file);
  408.  
  409. // Threads
  410. #if USE_PTHREAD
  411. typedef void* ( *beginthread_type )( void * );
  412. unsigned long _beginthread( beginthread_type start_address, unsigned stack_size, void *arglist );
  413. #endif
  414.  
  415. /*
  416. #ifdef __cplusplus
  417. }
  418. #endif
  419. */
  420.  
  421.  
  422.  
  423. /* variables globales */
  424. //extern LLint HTS_TOTAL_RECV;  // flux entrant reτu
  425. //extern int HTS_TOTAL_RECV_STATE;  // status: 0 tout va bien 1: ralentir un peu 2: ralentir 3: beaucoup
  426. extern hts_stat_struct HTS_STAT;
  427. extern int _DEBUG_HEAD;
  428. extern FILE* ioinfo;
  429.  
  430. /* constantes */
  431. extern const char hts_mime_keep[][32];
  432. extern const char hts_mime[][2][32];
  433. extern const char hts_detect[][32];
  434. extern const char hts_detectbeg[][32];
  435. extern const char hts_nodetect[][32];
  436. extern const char hts_detectURL[][32];
  437. extern const char hts_detectandleave[][32];
  438. extern const char hts_detect_js[][32];
  439.  
  440. // defaut wrappers
  441. void  __cdecl htsdefault_init(void);
  442. void  __cdecl htsdefault_uninit(void);
  443. int   __cdecl htsdefault_start(void* opt);
  444. int   __cdecl htsdefault_chopt(void* opt);
  445. int   __cdecl htsdefault_end(void);
  446. int   __cdecl htsdefault_checkhtml(char* html,int len,char* url_adresse,char* url_fichier);
  447. int   __cdecl htsdefault_loop(void* back,int back_max,int back_index,int lien_n,int lien_tot,int stat_time,hts_stat_struct* stats);
  448. char* __cdecl htsdefault_query(char* question);
  449. char* __cdecl htsdefault_query2(char* question);
  450. char* __cdecl htsdefault_query3(char* question);
  451. int   __cdecl htsdefault_check(char* adr,char* fil,int status);
  452. void  __cdecl htsdefault_pause(char* lockfile);
  453. void  __cdecl htsdefault_filesave(char*);
  454. int   __cdecl htsdefault_linkdetected(char* link);
  455. int   __cdecl htsdefault_xfrstatus(void* back);
  456. // end defaut wrappers
  457.  
  458. #endif
  459.  
  460.  
  461.